package com.atguigu.lease.web.admin.service.Impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-11
 * @Version: 1.0
 */

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    GraphInfoService graphInfoService;

    @Resource
    RoomInfoMapper roomInfoMapper;

    @Resource
    ApartmentMapper apartmentMapper;

    @Resource
    AttrValueMapper attrValueMapper;

    @Resource
    FacilityMapper facilityMapper;

    @Resource
    LabelInfoMapper labelInfoMapper;

    @Resource
    PaymentTypeMapper paymentTypeMapper;

    @Resource
    LeaseTermMapper leaseTermMapper;

    @Resource
    GraphInfoMapper graphInfoMapper;

    //保存或更新公寓信息
    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo submitVo) {

        if (submitVo.getId() != null){

            //1.删除属性信息
            LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,submitVo.getId());
            roomAttrValueService.remove(attrValueLambdaQueryWrapper);

            //2.删除配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,submitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            //3.删除标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,submitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            //4.删除支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,submitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //5.删除租赁信息
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,submitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

            //6.删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,submitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //然后开始添加 没有id 就先保存
            super.saveOrUpdate(submitVo);

            //1.保存属性信息
            List<Long> attrValueIds = submitVo.getAttrValueIds();
            if (!CollectionUtils.isEmpty(attrValueIds)){
                List<RoomAttrValue> attrValueList = new ArrayList<>();
                for (Long attrValueId : attrValueIds) {
                    RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                            .roomId(submitVo.getId())
                            .attrValueId(attrValueId)
                            .build();
                    attrValueList.add(roomAttrValue);
                }
                roomAttrValueService.saveBatch(attrValueList);
            }

            //2.保存配套信息
            List<Long> facilityInfoIds = submitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)){
                List<RoomFacility> roomFacilityList = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    RoomFacility roomFacility = RoomFacility.builder()
                            .roomId(submitVo.getId())
                            .facilityId(facilityInfoId)
                            .build();
                    roomFacilityList.add(roomFacility);
                }
                roomFacilityService.saveBatch(roomFacilityList);
            }

            //3.保存标签信息
            List<Long> labelInfoIds = submitVo.getLabelInfoIds();
            if (!CollectionUtils.isEmpty(labelInfoIds)){
                List<RoomLabel> roomLabelList = new ArrayList<>();
                for (Long labelInfoId : labelInfoIds) {
                    RoomLabel roomLabel = RoomLabel.builder()
                            .roomId(submitVo.getId())
                            .labelId(labelInfoId)
                            .build();
                    roomLabelList.add(roomLabel);
                }
                roomLabelService.saveBatch(roomLabelList);
            }

            //4.保存支付方式信息
            List<Long> paymentTypeIds = submitVo.getPaymentTypeIds();
            if (!CollectionUtils.isEmpty(paymentTypeIds)){
                List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
                for (Long paymentTypeId : paymentTypeIds) {
                    RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                            .roomId(submitVo.getId())
                            .paymentTypeId(paymentTypeId)
                            .build();
                    roomPaymentTypeList.add(roomPaymentType);
                }
                roomPaymentTypeService.saveBatch(roomPaymentTypeList);
            }

            //5.保存租赁信息
            List<Long> leaseTermIds = submitVo.getLeaseTermIds();
            if (!CollectionUtils.isEmpty(leaseTermIds)){
                List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
                for (Long leaseTermId : leaseTermIds) {
                    RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                            .roomId(submitVo.getId())
                            .leaseTermId(leaseTermId)
                            .build();
                    roomLeaseTermList.add(roomLeaseTerm);
                }
                roomLeaseTermService.saveBatch(roomLeaseTermList);
            }

            //6.保存图片信息
            List<GraphVo> graphVoList = submitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)){
                List<GraphInfo> graphInfoList = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setItemType(ItemType.ROOM);
                    graphInfo.setItemId(submitVo.getId());
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfoList.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfoList);
            }
        }
    }

    //分页查询
    @Override
    public IPage<RoomItemVo> PageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo roomQueryVo) {

        return roomInfoMapper.PageRoomItemByQuery(page,roomQueryVo);
    }

    //根据id获取房间详细信息
    @Override
    public RoomDetailVo getRoomDetailById(Long id) {

        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //1.查询房间
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //2.查询所属公寓
        roomDetailVo.setApartmentInfo(apartmentMapper.selectById(roomInfo.getApartmentId()));

        //3.查询属性
        roomDetailVo.setAttrValueVoList(attrValueMapper.selectListByRoomId(id));

        //4.查询配套
        roomDetailVo.setFacilityInfoList(facilityMapper.selectListByRoomId(id));

        //5.查询标签
        roomDetailVo.setLabelInfoList(labelInfoMapper.selectListByRoomId(id));

        //6.查询支付方式
        roomDetailVo.setPaymentTypeList(paymentTypeMapper.selectListByRoomId(id));

        //7.查询租期信息
        roomDetailVo.setLeaseTermList(leaseTermMapper.selectListByRoomId(id));

        //8.查询图片信息
        roomDetailVo.setGraphVoList(graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,id));


        return roomDetailVo;
    }

    //根据id删除房间
    @Override
    @Transactional
    public void removeRoomById(Long id) {

        //1.删除房间信息
        roomInfoMapper.deleteById(id);

        //2.删除属性信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //3.删除配套信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        //4.删除标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //5.删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //6.删除租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        //7.删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}
